//可否下拉
var is_bool=true;
// 页码信息
var page_no=1;
//选中的产品id
var choose_id=[];

$(function() {
    getMyCollect();
    
    $(window).scroll(function(){
    　　var scrollTop = $(this).scrollTop();
    　　var scrollHeight = $(document).height();
    　　var windowHeight = $(this).height();
    　　if(scrollTop + windowHeight == scrollHeight&&is_bool){
    　　　　getMyCollect();
    　　}
    });

    $('.edit').click(function(){
        $('.edit').hide();
        $('.over').show();
        $('.foot').show();
        $('.radio').show();
        $('.tip').show();
        is_bool=false;
    });
    $('.over').click(function(){
        $('.over').hide();
        $('.foot').hide();
        $('.radio').hide();
        $('.tip').hide();
        $('.edit').show();
        is_bool=true;
    });

    //取消
    $('.cancel-collect').click(function(){
        cancelCollect();
    });
})



function getMyCollect() {
    //禁止滚动
    is_bool=false;
    $('.product').show();
    //搜索结果
    $('.search-result lable').eq(0).html('?');
    $('.load').show();
    $.ajax({
        type: "post",
        url: "Api/Product/getMyCollect",
        data: {
            park_id: $('#park_id').val(),
            openid:$('#openid').val(),
            page_no:page_no
        },
        async: true,
        dataType: 'json',
        success: function(data) {
            if (data.status) {
                $('.search-result').show();
                setTimeout(function(){
                    $('.load').hide();
                    var t = JSON.parse(data.v);
                    $('.search-result lable').eq(0).html(t.count);
                    $.each(t.product, function(k, v) {
                        // alert(v.img[0]);
                        $('.product-demo a').attr('href',getRootPath()+'/Home/Product/product?park_id='+$('#park_id').val()+'&product='+v.id);
                        if(v.img[0]){
                           $('.product-demo .product-img img').attr('src',v.img[0]).attr('data-src',v.img[0]); 
                        }
                        else{
                            $('.product-demo .product-img img').attr('src','public/home/images/no-img.jpg').attr('data-src','public/home/images/no-img.jpg'); 
                        }
                        
                        $('.product-demo .product-num').html(sub_str(v.code,7));
                        $('.product-demo .product-collection lable').html(v.type);
                        if(v.is_collect){
                            $('.product-demo .product-collection i').attr('class','fa fa-heart');
                        }
                        else{
                            $('.product-demo .product-collection i').attr('class','fa fa-heart-o');
                        }   
                        // $('.product-demo .product-line-1').html(v.type);
                        $('.product-demo .product-line-2').html(v.series+'系列');
                        $('.product-demo .product-line-3 lable').html(v.long+'×'+v.wide+'×'+v.high);
                        $('.product-demo .radio').attr('data-id',v.id);
                        $('.product').append($('.product-demo').html());
                        $('.product .product-item:last').addClass('product-item-'+v.id);
                    });
                    is_bool=t.have;
                    page_no++;
                },1000);
                lazyLoad();
                $(window).scroll();
            } else {

            }
        }
    });
}


function chooseId(obj){
    if($(obj).attr('data-choose')=='0'){
        choose_id.push($(obj).attr('data-id'));
        $(obj).addClass('radio-choose');
        $(obj).attr('data-choose','1');
    }
    else{
        for (var i =0 ; i <choose_id.length; i++) {
            if(choose_id[i]==$(obj).attr('data-id')){
                choose_id.splice(i, 1);
                break;
            }
        }
        $(obj).removeClass('radio-choose');
        $(obj).attr('data-choose','0');
    }
}


function cancelCollect(){
    if(choose_id.length==0){
        errorAlert('请选择产品');
        return;
    }
    $.ajax({
        type: "post",
        url: "Api/Product/cancelCollect",
        data: {
            openid:$('#openid').val(),
            choose_id:choose_id.join(',')
        },
        async: true,
        dataType: 'json',
        success: function(data) {
            if (data.status) {
                for (var i =0 ; i <choose_id.length; i++) {
                    $('.product .product-item-'+choose_id[i]).remove();
                }
                rightAlert(data.msg);
            } else {
                errorAlert(data.msg);
            }
        }
    });
}